package com.haochang.chunkvod.controller.fragment.hostess;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseFragment;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;

/**
 *         created at 2016/8/27 17:03
 *         桌台设置页面
 */
public class DeskSettingFragment extends BaseFragment implements KeyBoardDialog.KeyBoardOnSureListener
{
	private BaseEditText et_count, et_desc;
	private String remarks = "";
	private String billId;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_desk_setting, container, false);
		initView(view);
		return view;
	}

	private void initView(View view)
	{
		RelativeLayout group_count = (RelativeLayout) view.findViewById(R.id.group_count);//设置人数
		Button desk_setting_ok = (Button) view.findViewById(R.id.desk_setting_ok);//确定按钮
		et_count = (BaseEditText) view.findViewById(R.id.et_count);
		et_desc = (BaseEditText) view.findViewById(R.id.et_desc);

		et_count.setOnClickListener(onBaseClickListener);
		group_count.setOnClickListener(onBaseClickListener);
		desk_setting_ok.setOnClickListener(onBaseClickListener);

		et_count.addTextChangedListener(new TextWatcher()
		{
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
				String countStr = et_count.getText().toString().trim();
				if (!TextUtils.isEmpty(countStr) && Integer.parseInt(countStr) > 1000)
				{
					ToastUtils.showText(getActivity(), "房间人数超过1000");
					et_count.setText("");
				}
			}

			@Override
			public void afterTextChanged(Editable s)
			{

			}
		});

		billId = getArguments().getString(IntentKey.BILL_ID);
		String comstmerCount = getArguments().getString(IntentKey.COMSTMER_COUNT);
		remarks = getArguments().getString(IntentKey.REMARKS);
		if (!TextUtils.isEmpty(comstmerCount) && Integer.parseInt(comstmerCount) > 0)
		{
			et_count.setText(comstmerCount);
		}
		if (!TextUtils.isEmpty(remarks))
		{
			et_desc.setText(remarks);
			et_desc.setSelection(remarks.length());
		}
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.et_count:
			case R.id.group_count:
				KeyBoardDialog keyBoardDialog = new KeyBoardDialog(getActivity());
				keyBoardDialog.setKeyBoardOnSureListener(DeskSettingFragment.this);
				keyBoardDialog.show();
				break;
			case R.id.desk_setting_ok:
				//设置桌台
				String costomerCount = et_count.getText().toString().trim();
				if (TextUtils.isEmpty(costomerCount) || Integer.parseInt(costomerCount) <= 0)
				{
					ToastUtils.showText(getActivity(), "请输入人数");
					return;
				}
				remarks = et_desc.getText().toString().trim();
				if (!TextUtils.isEmpty(billId))
				{
					new HostessData((BaseActivity) getActivity()).requestDeskSetting(billId, costomerCount, remarks, new HostessData.NoticeListener()
					{
						@Override
						public void onRequestSuccess(String notice)
						{
							if (!TextUtils.isEmpty(notice))
							{
								ToastUtils.showText(getActivity(), "修改成功");
								getActivity().finish();
							}
						}

						@Override
						public void onFail(int errno, String errMsg)
						{

						}
					});
				}
				break;
			}
		}
	};

	@Override
	public void setValue(String value)
	{
		try
		{
			if (value.contains("."))
				ToastUtils.showText(getActivity(), R.string.error_number);
			else if (Integer.parseInt(value) <= 0)
				ToastUtils.showText(getActivity(), R.string.error_number);
			else
			{
				et_count.setText(value);
			}
		} catch (Exception e)
		{
			ToastUtils.showText(getActivity(), getResources().getString(R.string.error_max_number));
		}
	}
}
